#!/bin/bash

# Session name
session="qemu"

# img path
img_path="../out/qemu-arm-linux/packages/phone/images"
export KERNEL_TEST_ROOT=`pwd`

check_tools() {
  which qemu-system-arm > /dev/null
  if [[ $? == 1 ]]; then
    echo "qemu-system-arm not found, please install it"
    exit 1
  fi

  which hdc > /dev/null
  if [[ $? == 1 ]]; then
    echo "hdc not found, please install it"
    exit 1
  fi

  which tmux > /dev/null
  if [[ $? == 1 ]]; then
    echo "tmux not found, please install it"
    sudo apt-get install tmux
  fi
}

setting_bridge() {
  ifconfig br0 > /dev/null 2>&1
  if [ $? -ne 0 ]; then
      qemu_install_path='/usr/local'
      sudo modprobe tun tap
      sudo ip link add br0 type bridge
      sudo ip address add 10.0.2.2/24 dev br0
      sudo ip link set dev br0 up
      [ ! -d ${qemu_install_path}/etc/qemu ] && sudo mkdir -p ${qemu_install_path}/etc/qemu
      if [[ $(cat ${qemu_install_path}/etc/qemu/bridge.conf) ==  "allow br0" ]]; then
          echo 'allow br0' | sudo tee -a ${qemu_install_path}/etc/qemu/bridge.conf
      fi
      echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
      sudo brctl addbr br0
      sudo ifconfig br0 up
      sudo ifconfig br0 10.0.2.2/24
  fi
}

start_qemu() {
  tmux has -t $session
  if [[ $? == 1 ]]; then
    tmux new -d -s $session
  else
    tmux kill-session -t $session
    tmux new -d -s $session
  fi

  setting_bridge

  startup_params="sudo qemu-system-arm -M virt -cpu cortex-a7 -smp 4 -m 1024 -nographic \
                  -netdev bridge,id=net0 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
                  -drive if=none,file=$img_path/userdata.img,format=raw,id=userdata,index=3 -device virtio-blk-device,drive=userdata \
                  -drive if=none,file=$img_path/vendor.img,format=raw,id=vendor,index=2 -device virtio-blk-device,drive=vendor \
                  -drive if=none,file=$img_path/system.img,format=raw,id=system,index=1 -device virtio-blk-device,drive=system \
                  -drive if=none,file=$img_path/updater.img,format=raw,id=updater,index=0 -device virtio-blk-device,drive=updater \
                  -kernel $img_path/zImage-dtb \
                  -initrd $img_path/../../../ramdisk.img \
                  -append \"console=ttyAMA0,115200 init=/bin/init hardware=qemu.arm.linux default_boot_device=a003e00.virtio_mmio root=/dev/ram0 rw\" "

  tmux send-keys -t qemu "$startup_params" C-m
  tmux send-keys -t qemu "123456" C-m # input sudo password
  sleep 20s # wait for qemu to boot
}

connect_to_qemu()
{
  tmux send-keys -t qemu "" C-m
  tmux send-keys -t qemu "ifconfig eth0 10.0.2.200 netmask 255.255.255.0" C-m
  tmux send-keys -t qemu "param set persist.hdc.mode tcp" C-m
  tmux send-keys -t qemu "param set persist.hdc.port 10086" C-m
  tmux send-keys -t qemu "hdcd -t" C-m
  sleep 10s # wait for hdcd start sever

  hdc tconn 10.0.2.200:10086
  hdc shell ls
}

test_folder_in_board()
{
   hdc shell " if test  -d /data/kernel_test ; then echo "/data/kernel_test exist"> /dev/null 2>&1;  else  mkdir /data/kernel_test; fi"
}

is_busybox_armv7l_exist()
{
   if [  -f ${KERNEL_TEST_ROOT}/busybox_armv7l ]; then
      echo "there is no busybox_armv7l"
      exit 1
   fi
}

send_test_folder()
{
   hdc file send ${KERNEL_TEST_ROOT}/kernel_test_auto.sh /data/kernel_test > /dev/null 2>&1
   hdc file send ${KERNEL_TEST_ROOT}/busybox-armv7l /data/kernel_test > /dev/null 2>&1

    hdc shell "if test  -d /data/kernel_test/shell_test ; then 
                   echo "/data/kernel_test/shell_test exist"> /dev/null 2>&1;  
	       else 
		   mkdir /data/kernel_test/shell_test; fi"   
   hdc file send ${KERNEL_TEST_ROOT}/shell_test/runtest.tar.gz  /data/kernel_test/shell_test > /dev/null 2>&1

   hdc shell "if test -d /data/kernel_test/binary_test; then 
                  echo "/data/kernel_test/binary_test exist"> /dev/null 2>&1;  
	      else
		  mkdir /data/kernel_test/binary_test; fi"   
   cd ${KERNEL_TEST_ROOT}
   ls ${KERNEL_TEST_ROOT}/binary_test > a
   local binary_file_num=`wc -l a | awk '{print $1}'`
         for i in $(seq 1 ${binary_file_num})
         do
             local binary_file_name=`sed -n ${i}p a`
             hdc file send ${KERNEL_TEST_ROOT}/binary_test/${binary_file_name}  /data/kernel_test/binary_test > /dev/null 2>&1
         done
   rm a
}

do_install_busybox_armv7l()
{
   hdc shell "mount -o rw,remount /"
   hdc shell "cp /data/kernel_test/busybox-armv7l /system/bin"  
   hdc shell "chmod 755 /system/bin/busybox-armv7l"
   hdc shell "cd /system/bin; busybox-armv7l  --install ."
   hdc shell "rm -rf /system/bin/tr"
   hdc shell "ln -s /system/bin/busybox-armv7l /system/bin/tr"
   hdc shell "mount -o ro,remount /"
}

do_test_shell()
{
   echo -e “ START SHELL_TEST ”
   hdc shell "cd /data/kernel_test/shell_test/; tar -zxvf runtest.tar.gz" > /dev/null 2>&1  
   hdc shell "rm -rf /data/kernel_test/shell_test/runtest/log/*"
   hdc shell "cd /data/kernel_test/shell_test/runtest; eval sh runtest test -t OH_${board_name}_config -n OH_${board_name}_skiptest -l ${time_limit}" 
}

do_test_binary()
{
   echo -e "\n"
   echo -e “ START BINARY_TEST ”
   cd ${KERNEL_TEST_ROOT}
   ls ${KERNEL_TEST_ROOT}/binary_test > a 
   local binary_file_num=`wc -l a | awk '{print $1}'`
      for i in $(seq 1 ${binary_file_num})
      do 
      local binary_file_name=`sed -n ${i}p a`
      hdc shell "cd /data/kernel_test/binary_test; 
                 chmod +x ${binary_file_name}; ./${binary_file_name}" | tee ${KERNEL_TEST_ROOT}/log/binary_log/log_$binary_file_name
      done
   rm a
}

do_get_shell_log()
{
   rm -rf ${KERNEL_TEST_ROOT}/log/shell_log/*
   hdc shell "cd /data/kernel_test/shell_test/runtest/log/;
         tar -zcvf shell_log.tar.gz *"  > /dev/null 2>&1 
   hdc file recv /data/kernel_test/shell_test/runtest/log/shell_log.tar.gz ${KERNEL_TEST_ROOT}/log/shell_log   > /dev/null 2>&1
   cd ${KERNEL_TEST_ROOT}/log/shell_log
   tar -zxvf ${KERNEL_TEST_ROOT}/log/shell_log/shell_log.tar.gz  > /dev/null 2>&1
   rm ${KERNEL_TEST_ROOT}/log/shell_log/shell_log.tar.gz
}

quit_qemu()
{
   sudo killall qemu-system-arm
   tmux kill-session -t qemu
}

main()
{
  check_tools
  start_qemu
  connect_to_qemu
  test_folder_in_board
  is_busybox_armv7l_exist
  send_test_folder
  do_install_busybox_armv7l
  echo "the test start"
  if [ "$1" == "all" ]; then
      do_test_shell
      do_test_binary    
      do_get_shell_log
  elif [ "$1" == "shell" ]; then
      do_test_shell
      do_get_shell_log
  elif [ "$1" == "binary" ]; then
      do_test_binary
  fi
  echo "the test end"
  quit_qemu
}
main "$@"